package utils;

import book.Book;
import com.bit.utils.FileUtils;
import constant.Constant;

import java.io.IOException;
import java.time.LocalDate;

public class AnalyzingBook {
    public void storeBook(Book[] books, String filename) throws IOException {
        StringBuilder jsonArray = new StringBuilder();
        int booksUseLen = 0;
        for (int i = 0; i < books.length; i++) {
            if(books[i] != null){
                booksUseLen++;
            }
        }

        for (int i = 0; i < books.length; i++) {
            if(books[i] != null){
                jsonArray.append(books[i].toJson());
                if(i != booksUseLen-1){
                    jsonArray.append('\n');
                }
            }
        }

        FileUtils.writeFile(jsonArray.toString(), filename);
    }

    public Book[] loadBook(String filename) throws IOException{
        String content = FileUtils.readFile(Constant.ALL_BOOK_NAME_FILE);
        if(content == null || content.isEmpty()){
            return null;
        }

        String[] bookJsonString = content.split("\n");
        Book[] books = new Book[bookJsonString.length];
        for (int i = 0; i < bookJsonString.length; i++){
            books[i] = parseBookJson(bookJsonString[i]);
        }

        return books;
    }

    private Book parseBookJson(String pair) {

        if(pair.isEmpty()){
            return null;
        }

        String[] pairs = pair.split(",");
        int bookId = Integer.parseInt(pairs[0]);
        String title = pairs[1];
        String author = pairs[2];
        String category = pairs[3];
        int publishYear = Integer.parseInt(pairs[4]);
        boolean isBorrowed = Boolean.parseBoolean(pairs[5]);
        int borrowCount = Integer.parseInt(pairs[6]);
        LocalDate localDate = LocalDate.parse(pairs[7]);

        if(title != null && author != null && category != null && localDate != null){
            Book book = new Book(title, author, category, publishYear, localDate);
            book.setBookId(bookId);
            book.setBorrowed(isBorrowed);
            book.setBorrowCount(borrowCount);
            return book;
        }
        return null;
    }

    public static void main1(String[] args) {
        Book[] books = new Book[5];
        /*Book book1 = new Book("Java", "高斯林", "编程", 1995, LocalDate.of(2025,8,12));
        Book book2 = new Book("Mysql", "ben", "编程", 1994, LocalDate.of(2025,8,13));
        Book book3 = new Book("数据结构", "kay", "编程", 1996, LocalDate.of(2025,8,12));
        books[0] = book1;
        books[1] = book2;
        books[2] = book3;
        Book book4 = new Book("Linux", "Liam", "编程", 1997, LocalDate.of(2025,8,12));
        books[3] = book4;*/

        AnalyzingBook analyzingBook = new AnalyzingBook();
        try {
            // analyzingBook.storeBook(books, Constant.ALL_BOOK_NAME_FILE);
            Book[] bookL= analyzingBook.loadBook(Constant.ALL_BOOK_NAME_FILE);
            for (Book b:bookL) {
                System.out.println(b);
            }
        }catch (IOException e){
            throw new RuntimeException();
        }

    }
}
